package de.dlrg.wms.gui.forms;

import static de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames.AGE_CLASS_COLUMN;
import static de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames.DISCIPLINE_COLUMN;
import static de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames.RUN_NO_COLUMN;
import static de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames.SWIMMER_COLUMN;
import static de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames.TIME_COLUMN;
import static de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames.TRACK_COLUMN;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import swing2swt.layout.BorderLayout;
import de.dlrg.wms.controller.controllers.CompetitionController;
import de.dlrg.wms.controller.controllers.RunController;
import de.dlrg.wms.controller.controllers.interfaces.ICompetitionCallback;
import de.dlrg.wms.gui.forms.customForms.CurrentRunComposite;
import de.dlrg.wms.gui.views.coulmnNames.NextRunColumnNames;
import de.dlrg.wms.model.CompetitionStatus;
import de.dlrg.wms.model.Run;
import de.dlrg.wms.model.SwimmerTrackRun;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;

public class CompetitionForm extends Composite implements ICompetitionCallback {
	private Label lblCompetitionstate;
	private Composite composite;

	private Composite shell;

	private CompetitionController competitionController;
	private RunController runController;
	private CompetitionStatus competitionStatus;
	private CurrentRunComposite currentRun;
	private Table nextRuns;
	private Text txtRunnotxt;

	@Override
	public void dispose() {
		this.competitionController.removeUpdateListener(this);
		super.dispose();
	}

	private void refresh() {
		this.competitionStatus = this.competitionController
				.getCompetitionStatus();

		this.txtRunnotxt.setText(""
				+ this.competitionStatus.getCurrentRun().getRunNo());
		this.lblCompetitionstate.setText(""
				+ this.competitionStatus.getRunState().toString());

		this.refreshNextRuns();
		this.refreshCurrentRun();
	}

	private void refreshCurrentRun() {
		this.currentRun.setCurrentRun(this.competitionStatus.getCurrentRun());

	}

	private void refreshNextRuns() {
		this.nextRuns.clearAll();
		this.nextRuns.setItemCount(0);

		List<Run> runs = this.runController.readAll();
		for (Run run : runs) {
			this.addRun(run, this.nextRuns);
		}
	}

	private List<TableItem> addRun(Run run, Table table) {
		List<TableItem> tableItems = new ArrayList<TableItem>();
		TableItem runTableItem = this.addSwimmerTrackRunData(run
				.getSwimmerTrackRun().get(0), table);
		runTableItem.setData(run);

		runTableItem.setText(RUN_NO_COLUMN.ordinal(), "" + run.getRunNo());

		for (int i = 1; i < run.getSwimmerTrackRun().size(); i++) {
			this.addSwimmerTrackRunData(run.getSwimmerTrackRun().get(i), table)
					.setData(run);
		}
		return tableItems;
	}

	private TableItem addSwimmerTrackRunData(SwimmerTrackRun swimmerTrackRun,
			Table table) {
		TableItem swimmerTrackRunTableItem = new TableItem(table, SWT.NULL);
		{
			swimmerTrackRunTableItem.setText(AGE_CLASS_COLUMN.ordinal(),
					swimmerTrackRun.getSwimmer().getAgeClass().getName());
			swimmerTrackRunTableItem.setText(SWIMMER_COLUMN.ordinal(),
					swimmerTrackRun.getSwimmer().getName() + ", "
							+ swimmerTrackRun.getSwimmer().getSurName());
			swimmerTrackRunTableItem.setText(DISCIPLINE_COLUMN.ordinal(),
					swimmerTrackRun.getDiscipline().getName());
			swimmerTrackRunTableItem.setText(TRACK_COLUMN.ordinal(), ""
					+ swimmerTrackRun.getTrack().getTrackNumber());
			swimmerTrackRunTableItem.setText(TIME_COLUMN.ordinal(), ""
					+ swimmerTrackRun.getTime());
			if (swimmerTrackRun.isStopped()) {
				Display display = Display.getCurrent();
				Color green = display.getSystemColor(SWT.COLOR_GREEN);
				swimmerTrackRunTableItem.setBackground(green);
			}
			// swimmerTrackRunTableItem.setText(STOPPED.ordinal(), "" +
			// swimmerTrackRun.isStopped());
		}
		return swimmerTrackRunTableItem;
	}

	public CompetitionForm(Composite arg0, int arg1,
			final CompetitionController competitionController,
			RunController runController) {
		super(arg0, arg1);
		this.shell = arg0;
		this.competitionController = competitionController;
		this.runController = runController;

		setLayout(new BorderLayout(0, 0));

		composite = new Composite(this, SWT.NONE);
		composite.setLayoutData(BorderLayout.NORTH);
		composite.setLayout(new FormLayout());

		Button btnPause = new Button(composite, SWT.NONE);
		btnPause.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				competitionController.startStopCompetition(true);
			}
		});
		FormData fd_btnPause = new FormData();
		fd_btnPause.top = new FormAttachment(0, 10);
		fd_btnPause.right = new FormAttachment(100, -10);
		btnPause.setLayoutData(fd_btnPause);
		btnPause.setText("Pause");

		Button resetTimeOfSelectedSwimmer = new Button(composite, SWT.NONE);
		resetTimeOfSelectedSwimmer.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				for (SwimmerTrackRun swimmerTrackRun : currentRun
						.getSelectedSwimmerTrackRun()) {
					competitionController.resetTime(swimmerTrackRun);
				}
			}
		});
		FormData fd_btnnd = new FormData();
		fd_btnnd.top = new FormAttachment(btnPause, 0, SWT.TOP);
		fd_btnnd.right = new FormAttachment(btnPause, -6);
		resetTimeOfSelectedSwimmer.setLayoutData(fd_btnnd);
		resetTimeOfSelectedSwimmer.setText("2nd");

		Label lblRunNo = new Label(composite, SWT.NONE);
		FormData fd_lblRunNo = new FormData();
		fd_lblRunNo.top = new FormAttachment(btnPause, 0, SWT.TOP);
		fd_lblRunNo.left = new FormAttachment(0, 10);
		lblRunNo.setLayoutData(fd_lblRunNo);
		lblRunNo.setText("Run No.:");

		txtRunnotxt = new Text(composite, SWT.BORDER);
		txtRunnotxt.setText("runNoTxt");
		FormData fd_txtRunnotxt = new FormData();
		fd_txtRunnotxt.bottom = new FormAttachment(lblRunNo, 0, SWT.BOTTOM);
		fd_txtRunnotxt.left = new FormAttachment(lblRunNo, 6);
		txtRunnotxt.setLayoutData(fd_txtRunnotxt);

		Label lblState = new Label(composite, SWT.NONE);
		FormData fd_lblState = new FormData();
		fd_lblState.bottom = new FormAttachment(lblRunNo, 0, SWT.BOTTOM);
		fd_lblState.left = new FormAttachment(txtRunnotxt, 6);
		lblState.setLayoutData(fd_lblState);
		lblState.setText("State:");

		lblCompetitionstate = new Label(composite, SWT.NONE);
		FormData fd_lblCompetitionstate = new FormData();
		fd_lblCompetitionstate.right = new FormAttachment(
				resetTimeOfSelectedSwimmer, -110);
		fd_lblCompetitionstate.top = new FormAttachment(btnPause, 0, SWT.TOP);
		fd_lblCompetitionstate.left = new FormAttachment(lblState, 6);
		lblCompetitionstate.setLayoutData(fd_lblCompetitionstate);
		lblCompetitionstate.setText("competitionState");

		Composite currentRunComposition = new Composite(this, SWT.NONE);
		currentRunComposition.setLayoutData(BorderLayout.CENTER);
		currentRunComposition.setLayout(new FormLayout());

		this.currentRun = new CurrentRunComposite(currentRunComposition,
				SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table_1 = new FormData();
		fd_table_1.bottom = new FormAttachment(0, 119);
		fd_table_1.top = new FormAttachment(0);
		fd_table_1.right = new FormAttachment(100);
		fd_table_1.left = new FormAttachment(0);
		this.currentRun.setLayoutData(fd_table_1);

		this.nextRuns = new Table(currentRunComposition, SWT.BORDER
				| SWT.FULL_SELECTION);
		FormData fd_table_2 = new FormData();
		fd_table_2.bottom = new FormAttachment(100, -10);
		fd_table_2.top = new FormAttachment(this.currentRun, 6);
		fd_table_2.left = new FormAttachment(0);
		fd_table_2.right = new FormAttachment(100);
		this.nextRuns.setLayoutData(fd_table_2);
		this.nextRuns.setHeaderVisible(true);
		this.nextRuns.setLinesVisible(true);

		this.setColumnNames(this.getColumnNames(), this.nextRuns);
		this.competitionController.registerUpdateListener(this);
		this.refresh();
	}

	protected void setColumnNames(String[] columnNames, Table table) {
		for (String columnName : columnNames) {
			TableColumn tblclmnid = new TableColumn(table, SWT.NONE);
			tblclmnid.setWidth(100);
			tblclmnid.setText(columnName);
		}
	}

	private String[] getColumnNames() {
		String ret[] = new String[NextRunColumnNames.values().length];
		for (NextRunColumnNames columnName : NextRunColumnNames.values()) {
			ret[columnName.ordinal()] = columnName.toString();
		}
		return ret;
	}

	public void updated(CompetitionStatus competitionStatus) {
		this.shell.getDisplay().asyncExec(new Runnable() {
			public void run() {
				refresh();				
			}
		});
	}
}
